package com.starblink.login.linksocialguangaccount.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.user.login.LoginManager
import com.starblink.login.databinding.ActivityLinkSocialGuangAccountBinding
import com.starblink.rocketreserver.ThirdLoginMutation

/**
 * 连接三方登录账号和Guang账号
 */
@Route(path = RoutePage.Login.LINK_SOCIAL_GUANG_ACCOUNT)
class LinkSocialGuangAccountActivity :
    BaseTVMActivity<ActivityLinkSocialGuangAccountBinding, LinkSocialGuangAccountVM>() {
    @Autowired(name = LoginManager.KEY_THIRD_LOGIN_ACCOUNT)
    @JvmField
    var thirdLoginAccount: ThirdLoginMutation.ThirdLoginAccountV2? = null

    override fun initViewBinding(): ActivityLinkSocialGuangAccountBinding =
        ActivityLinkSocialGuangAccountBinding.inflate(layoutInflater)

    override fun initData() {
        super.initData()
    }

    override fun initView() {
        super.initView()
        if(thirdLoginAccount?.email.isNullOrEmpty().not()){
            viewBinding.etEmail.text = thirdLoginAccount?.email!!
        }

        viewBinding.etEmail.setOnEditInputListener {
            viewBinding.tvErrorHintEmail.visibleOrGone(false)
            viewBinding.btnBottom.isEnabled = isBottomBtnEnable()
        }
        viewBinding.etPassword.setOnEditInputListener {
            viewBinding.tvErrorHintPsw.visibleOrGone(false)
            viewBinding.btnBottom.isEnabled = isBottomBtnEnable()
        }

        viewBinding.btnBottom.isEnabled = isBottomBtnEnable()
        viewBinding.btnBottom.click {
            var res = LoginManager.validateEmail(viewBinding.etEmail.text, true)
            if(res != null){
                viewBinding.tvErrorHintEmail.visibleOrGone(true)
                viewBinding.tvErrorHintEmail.text = res
                return@click
            }

            res = LoginManager.validatePassword(viewBinding.etPassword.text)
            if(res != null){
                viewBinding.tvErrorHintPsw.visibleOrGone(true)
                viewBinding.tvErrorHintPsw.text = res
                return@click
            }

            viewModel.checkPasswordThenBind(viewBinding.etEmail.text, viewBinding.etPassword.text, thirdLoginAccount!!)
        }

        viewBinding.tvForgotPsw.click {
            navigationTo(RoutePage.Login.FORGOT_PSW_INPUT_EMAIL)
        }

        //用户协议
        viewBinding.ivPrivacyProtocol.isChecked = viewModel.isAgreePolicy
        viewBinding.ivPrivacyProtocol.setOnClickListener {
            viewModel.isAgreePolicy = !viewModel.isAgreePolicy
            viewBinding.ivPrivacyProtocol.isChecked = viewModel.isAgreePolicy
        }
        LoginManager.setPrivacyPolicy(viewBinding.tvPrivacyProtocol)
    }

    fun isBottomBtnEnable(): Boolean =
        viewBinding.etEmail.text.trim().isNotEmpty()
                && viewBinding.etPassword.text.trim().isNotEmpty()
                && viewBinding.ivPrivacyProtocol.isChecked

    override fun initObservable() {
        super.initObservable()
    }
}